package top.healthylife.gateway.platform.eye.api.api.zengzhifuwu;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.eye.api.config.SystemConfig;
import top.healthylife.gateway.platform.eye.api.util.OutHttpUtil;
import top.healthylife.gateway.platform.eye.dto.zengzhifuwu.EyeReqRiskDetailDto.req.EyeReqRiskDetailDto;
import top.healthylife.gateway.platform.eye.dto.zengzhifuwu.EyeReqRiskDetailDto.res.EyeResRiskDetailDto;

import java.util.Map;

/**
 * @author: GenUtil
 * @date: 2020-12-15 19:07:15
 */
@Slf4j
@Service
@ApiServiceExplain(price = 20, docUrl = "http://open.tianyancha.com/open/426", apiName = "天眼风险详情[426]", info = "可以通过企业风险ID和人风险ID获取企业或人员相关天眼风险详情，包括企业和人员自身/周边/预警风险信息详情")
public class EyeRiskDetailApi implements ParentFace<EyeReqRiskDetailDto, EyeResRiskDetailDto> {

    static final String URL = "http://open.api.tianyancha.com/services/v4/open/riskDetail";

    /**
     * 执行这个服务接口
     * 如果成功，返回成功结果，如果接口错误，直接抛异常即可
     *
     * @param eyeDto 请求参数
     * @return 查询的真实结果，如果是失败，则直接返回异常即可
     */
    @Override
    public EyeResRiskDetailDto execute(EyeReqRiskDetailDto eyeDto) {
        //可直接通过工具类获取这个对象
        SystemConfig o = (SystemConfig) SystemConfigUtils.get();
        Map map = OutHttpUtil.get(URL, BeanUtil.beanToMap(eyeDto), o, Map.class);
        if (!map.containsKey("type")) {
            throw new BusinessException("数据结构异常,Type不在期望值之内");
        }
        Integer type = null;
        try {
            type = Integer.parseInt(map.get("type").toString());
            EyeResRiskDetailDto eyeResRiskDetailDto = BeanUtil.mapToBean(map, EyeResRiskDetailDto.class, false);
            //判断是否包含指定的类型
            EyeResRiskDetailDto.class.getDeclaredField("dataListType" + type);
            return eyeResRiskDetailDto;
        } catch (NoSuchFieldException noSuchFieldException) {
            throw new BusinessException("Type数值异常:" + type);
        }
        catch (Exception e) {
            throw new BusinessException("数据结构异常,Type不在期望值之内");
        }

    }
}
